package com.guiwu.gteway.industryapp.Device.View;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatDialogFragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.guiwu.gteway.industryapp.Device.module.CameraInfo;
import com.guiwu.gteway.industryapp.R;

import java.util.List;

/**
 * Created by qiaoguan on 2018/4/3. 选择摄像头
 */

public class VideoPickerFragment extends AppCompatDialogFragment {
    private RecyclerView recyclerView;
    private MyViewHolder vh;
    private MyAdapter adapter;
    private MonitorActivity activityParent;
    private List<CameraInfo> cameraInfos;
    public static final String EXTRA_INDEX_VEDIO = "com.guiwu.gteway.industryapp.Device.View.VideoIndex";
    public static final int RESULT_CODE = 0x10;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityParent = (MonitorActivity)getActivity();
        cameraInfos = activityParent.getCameraInfos();
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        View v = LayoutInflater.from(getActivity()).inflate(R.layout.video_ficker_fragment,null);
        recyclerView = v.findViewById(R.id.video_picker_recycle_view);
        recyclerView.setAdapter(new MyAdapter(cameraInfos));
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        recyclerView.addItemDecoration(new android.support.v7.widget.DividerItemDecoration(activityParent,LinearLayoutManager.VERTICAL));

        return new AlertDialog.Builder(getActivity())
                .setTitle(R.string.video_picker)
                .setView(v)
                .create();
    }


    private void sendResult(int resultCode, int index){
        if (getTargetFragment() == null){
            return;
        }
        Intent intent = new Intent();
        intent.putExtra(EXTRA_INDEX_VEDIO,index);

        getTargetFragment().onActivityResult(getTargetRequestCode(),resultCode,intent);
    }

    public interface OnSelectIndexListener {
        void onSelectIndex(int index);
    }

    private OnSelectIndexListener listener;

    public void setOnSelectListener(OnSelectIndexListener listener){
        this.listener = listener;
    }


    private class MyViewHolder extends RecyclerView.ViewHolder{
        private TextView videoName;

        public MyViewHolder(View itemView) {
            super(itemView);
            initView(itemView);
        }

        private void initView(View itemView){
            videoName = itemView.findViewById(R.id.video_picker_item);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                  //  sendResult(RESULT_CODE,getAdapterPosition());
                    if (listener != null){
                        listener.onSelectIndex(getAdapterPosition());
                    }
                }
            });
        }

        public void bindData(String name){
            videoName.setText(name);
        }
    }

    private class MyAdapter extends RecyclerView.Adapter<MyViewHolder>{
        List<CameraInfo> videoList;

        public MyAdapter(List<CameraInfo> videoList){
            this.videoList = videoList;
        }

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.video_item,parent,false);
            return new MyViewHolder(itemView);
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            holder.bindData(videoList.get(position).getDeviceName());
        }

        @Override
        public int getItemCount() {
            return videoList.size();
        }
    }

}
